
include("shared.lua")

// CLASS SETUP .. if

GM.DeathsThisRound = { }

function GM:PlayerRequestClass( ply, class, disableMsg )

	if (ply:IsValid()) then
		if( !ply:Alive() or ply:GetPlayerClass().DisplayName == "Civilian" ) then
		
			ply:SetPlayerClass(team.GetClass(ply:Team())[class])
		
			if( !self.DeathsThisRound[ply:UniqueID()] ) then
				ply:Spawn() 
			else
				ply:ChatPrint("You have already played this round...")
			end
				
		end
	end
	
end

// end


function GiveMsg(name, msg)
	local recipient = nil
	for _,v in ipairs(player.GetAll()) do
		if(v:Name() == name) then recipient = v break end
	end
	
	if(recipient == nil) then timer.Simple(15.0, GiveMsg, name, msg) return end
	recipient:ChatPrint(msg)
end
function GM:PlayerConnect(name, ip)
	timer.Simple(15, GiveMsg, name, "This gamemode is under heavy development... Leave any comments/questions at a203.xi@gmail.com")
end


function GM:PlayerDeath(victim, dmg, killer)
	self.DeathsThisRound[victim:UniqueID()] = true
	
	victim:SetTeam(TEAM_DEAD)
	
	if(team.NumPlayers(TEAM_ALIVE) == 0 && self:InRound() == true) then
		self:RoundEndWithResult(TEAM_DEAD, "The zombies have taken the city!")
	end
end
function GM:PlayerShouldTakeDamage( victim, attacker )
	return true
end
function GM:EntityTakeDamage( ent, inflictor, attacker, dmgamt, dmginfo )
	//Msg("Entity '" .. ent:GetClass() .. "' taking " .. dmginfo:GetDamage() .. " dmg from '" .. attacker:GetClass() .. " (" .. inflictor:GetClass() .. " -- " .. dmginfo:GetDamageType() .. ")\n")
	if(ent:GetClass() == "npc_zombie2") then
		if(ent:Health() - dmginfo:GetDamage() <= 0) then
			self:ZombieDeath(ent)
			if(attacker:Team() == 1) then
				if(dmginfo:IsExplosionDamage() or inflictor:GetClass() == "weapon_crowbar") then
					attacker:AddFrags(2)
				else
					attacker:AddFrags(1)
				end
			end
		end
	end
end

function GM:ClearInactivePlayers()
	for k,v in team.GetPlayers(TEAM_ALIVE) do
		if v:GetPlayerClass().DisplayName == "Civilian" or v:Alive() == false then
			self.DeathsThisRound[v:UniqueID()] = true
			v:KillSilent()
			v:SetTeam(TEAM_DEAD)
		end
	end
	self:CheckRoundEnd()
end

function GM:OnRoundStart( )
	self.DeathsThisRound = {}
	
	for _,v in ipairs(player.GetAll()) do
		v:KillSilent()
		v:SetPlayerClass("Default")
		
		v:SetTeam(TEAM_ALIVE)
		v:SendLua( "GAMEMODE:ShowClassChooser(" .. TEAM_ALIVE .. ")")
	end
	
	timer.Simple(30.0, self.ClearInactivePlayers, self)
	
	for _,v in ipairs(ents.FindByClass("zombie_spawner")) do
		v:ResetSpawnVals()
		if(v.SpawnParam[v:EntIndex()].StartEnabled) then v:StartSpawning() end
	end

end
function GM:OnRoundEnd( count )
	//da humans win
	if( team.NumPlayers(TEAM_ALIVE) > 0 ) then
		team.AddScore(TEAM_ALIVE, 1)
		for _,v in ipairs(team.GetPlayers(TEAM_ALIVE)) do
			v:ChatPrint("Good job. You have earned 10 bonus points.")
			v:AddFrags(10)
		end
	end
	
end
function GM:ZombieDeath( npc )
	for _,v in ipairs(ents.FindByClass("zombie_spawner")) do
		if(table.HasValue(v.SpawnVals[v:EntIndex()].ActiveZombies, npc)) then
			for nk, nv in pairs(v.SpawnVals[v:EntIndex()].ActiveZombies) do
				if(nv == npc) then
					v:ConfirmDead(npc)
				end
			end
		end
	end
end

function GM:CheckRoundEnd()
	if (#player.GetAll() == 0) then self:RoundEnd() return end
	if (team.NumPlayers(TEAM_ALIVE) == 0) then self:RoundEndWithResult(TEAM_DEAD) return end
end